package de.logicline.cvss.util;


import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.internal.dialogs.ViewSorter;

public class TableBuilder {

	
	private TableViewer viewer;
	private Table table;

	public TableBuilder(Composite parent){
		table = new Table(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI
				| SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		GridData data = new GridData(GridData.FILL_BOTH);
		table.setLayoutData(data);
		TableLayout layout = new TableLayout();
		table.setLayout(layout);
		viewer = new TableViewer(table);
	}

	public TableBuilder(Composite parent, int style, boolean linesVisible, boolean headerVisible){
		table = new Table(parent, style);
		table.setHeaderVisible(headerVisible);
		table.setLinesVisible(linesVisible);
		GridData data = new GridData(GridData.FILL_BOTH);
		table.setLayoutData(data);
		TableLayout layout = new TableLayout();
		table.setLayout(layout);
		viewer = new TableViewer(table);
	}
	
	public void setStyle(int[] columnStyles){
		if ( table.getColumns().length != columnStyles.length){
			throw new BuilderException();
		}
		
	}
	
	public void setColumnNames(String[] columnNames){
		if ( table.getColumns().length != columnNames.length){
			throw new BuilderException();
		}
		for( int i=0; i<table.getColumns().length; i++){
			table.getColumns()[i].setText(columnNames[i]);
		}
	}
	
	public void setColumnWidths(int[] columnWidth){
		if ( table.getColumns().length != columnWidth.length){
			throw new BuilderException();
		}
		for( int i=0; i<table.getColumns().length; i++){
			table.getColumns()[i].setWidth(columnWidth[i]);
		}
	}
	
	public void createColumns(int count){
		if( table == null || viewer == null){
			throw new BuilderException();
		}
		for (int i=0; i< count; i++){
			TableColumn col = new TableColumn(table, SWT.NONE);
			col.setResizable(true);
		}
	}
	
	public void setColumnWeights(int[] columnWeights){
		
		TableLayout layout = new TableLayout();
		table.setLayout(layout);
		
		for (int i=0; i< columnWeights.length; i++){
			layout.addColumnData(new ColumnWeightData(columnWeights[i], true));
		}
		
	}
	
	public void setSorter(ViewSorter viewSorter) {
		
	}

	
	
	
	class BuilderException extends RuntimeException{
		
	}

	public TableViewer getViewer() {
		return viewer;
	}
}
